
var inUserNo = 1;
var loginUsers = [];
var sessionIds = {};

exports.add = function(dbConn,  unitInfo, io, sid) {
	var inUserCnt = assignUser(sid, unitInfo.USER_ID, loginUsers);
	console.log(inUserCnt);
	console.log("+++++++++++++");
	var addParam = "'" + unitInfo.USER_ID + "', ";
	addParam += unitInfo.UNIT_CD + ", ";
	addParam += "0, ";
	addParam += unitInfo.TERMS + ", ";
	addParam += "'I' ";
	var addSql = "call gsp_save_unit_growth(" + addParam + ");";
	//console.log(addSql);
	dbConn.query(
		addSql,
		function (err, rows) {
		if (err) throw err;
		//console.log(rows[0][0].UNIT_SEQ);
		unitInfo.UNIT_SEQ = rows[0][0].UNIT_SEQ;
		console.log("===================");
		io.sockets.socket(sid).emit('rsMsg', unitInfo);
	});
};

function assignUser(sid, userId, loginUsers) {
	sessionIds[userId] = sid;

	loginUsers.push(userId);
	return inUserNo+1;
};

exports.dupLoginUser = function(sid, userId, io) {
	//console.log(userIds);
	for (var sess in sessionIds) {
		console.log(sess);
		if (sess == userId) io.sockets.socket(sessionIds[sess]).emit('disconnected', "true");
		console.log("--------------");
	}
};

exports.unitMXSeq = function(dbConn, unitInfo) {
	var query = "SELECT IFNULL(MAX(UNIT_CD),0)+1 as MX_SEQ FROM G_UNIT_GROWTH WHERE USER_ID=? AND UNIT_CD=?";
	dbConn.query(
			query, 
			[unitInfo.USER_ID, unitInfo.UNIT_CD], 
			function (err, rows) {
				if (err) throw err;
				exports.getMxSeq(rows);
			}
		);
};

exports.getMxSeq = function(rows) {
	var maxSeq = 0; 
	maxSeq = rows[0].MX_SEQ;
	console.log(maxSeq);
	console.log("-----------getMxSeq---------------");
	return maxSeq;
}

exports.unitInfoAdd = function(unit, maxSeq) {
	//var unitJson = JSON.parse(unit);
	//console.log(unit.USER_ID);
	this.USER_ID = (unit.USER_ID) ? unit.USER_ID : '';
	this.UNIT_CD = (unit.UNIT_CD) ? unit.UNIT_CD : '';
	this.UNIT_SEQ = (maxSeq) ? maxSeq : 0;
	this.START_TM = (unit.START_TM) ? unit.START_TM : '';
	this.TERMS = (unit.TERMS) ? unit.TERMS : '';
	this.END_TM = (unit.END_TM) ? unit.END_TM : '';
	this.INCOME_TM = (unit.INCOME_TM) ? unit.INCOME_TM : '';
	this.STATUS = (unit.STATUS) ? unit.STATUS : '';
	this.TRASH_TM = (unit.TRASH_TM) ? unit.TRASH_TM : '';
	this.MAC = (unit.MAC) ? unit.MAC : '';
	this.IP = (unit.IP) ? unit.IP : '';
}

exports.parseReceivedData = function(req, cb) {
	var body = '';
	req.setEncoding('utf8');
	req.on('data', function(chunk) { body += chunk});
	req.on('end', function() {
		var data = qs.parse(body);
		cb(data);
	});
};


//exports.show = function(db, unitInfo) {
//
//	var query = "SELECT * FROM work WHERE archived=? ORDER BY date DESC";
//	var archiveValue = (showArchived) ? 1 : 0;
//	db.query(
//			query, 
//			[archiveValue],
//			function(err, rows) {
//				if(err) { console.log(err); throw err; }
//				var html = (showArchived) ? '' : '<a href="/archived"> Archived Work</a><br/>';
//				html += exports.workHitlistHtml(rows);
//				html += exports.workFormHtml();
//				exports.sendHtml(res,html);
//			}
//		);
//}